<?php
/**
 * This file houses the Protocol class.
 *
 * Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this 
 *   list of conditions and the following disclaimer in the documentation and/or other
 *   materials provided with the distribution.
 * - Neither the name of the Kalkomey Enterprises, Inc. nor the names of its 
 *   contributors may be used to endorse or promote products derived from this 
 *   software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @copyright Copyright (c) 2010, Kalkomey Enterprises, Inc.
 * @link http://code.google.com/p/swerv
 * @package Framework
 * @subpackage Library
 */

/**
 * The Protocol class loads the appropriate protocol files from the correct directories and provides pass-through access to the input and output functions.
 *
 * @package Framework
 * @subpackage Library
 */
class Protocol {
	
	/**
	 * The correct input protocol object.
	 *
	 * @var InputProtocol
	 */
	protected $inputProtocol;
	
	/**
	 * The correct output protocol object.
	 *
	 * @var OutputProtocol
	 */
	protected $outputProtocol;
	
	/**
	 * Object constructor.
	 *
	 * @uses Protocol::addProtocolPaths()
	 *
	 * @return Protocol
	 */
	public function __construct() {
		$this->inputProtocol = null;
		$this->outputProtocol = null;
		$this->addProtocolPaths();
		$this->loadProtocolObjects();
	}
	
	/**
	 * Sets the path to the currently requested protocol so the autoloader can find it when it is instantiated.
	 *
	 * @uses $paths['framework']
	 * @uses $apps
	 *
	 * @uses SiteContext::$protocol
	 *
	 * @return void
	 */
	protected function addProtocolPaths() {
		global $paths;
		$paths['framework']['current_protocol'] = $paths['shared']['protocols'] . '/' . SiteContext::$protocol;
		$paths['app']['current_protocol'] = $paths['app']['protocols'] . '/' . SiteContext::$protocol;
	}
	
	/**
	 * Loads the input and output protocol objects, ignoring any autoloading exceptions thrown.
	 *
	 * @uses StringUtility::toPascalCase()
	 *
	 * @return void
	 */
	protected function loadProtocolObjects() {
		$inputClassName = StringUtility::toPascalCase(SiteContext::$protocol . '_input_protocol');
		$outputClassName = StringUtility::toPascalCase(SiteContext::$protocol . '_output_protocol');
		try {
			$this->inputProtocol = new $inputClassName();
		} catch (Exception $e) {
			// do nothing!
		}
		try {
			$this->outputProtocol = new $outputClassName();
		} catch (Exception $e) {
			// do nothing!
		}
	}
	
	/**
	 * Returns any input that has been posted to the system according to the requested protocol.
	 *
	 * @uses InputProtocol::getInput()
	 *
	 * @throws ProtocolException if the InputProtocol object could not be found and loaded.
	 *
	 * @return array
	 */
	public function getRequestObject() {
		// need to use some reflection here to figure out what the request object name is -- hack for JSON weakness
		$reflection = new ReflectionClass(SiteContext::$controller);
		$reflectedMethod = $reflection->getMethod(SiteContext::$action);
		$parameters = $reflectedMethod->getParameters();
		$requestObjName = StringUtility::toPascalCase(StringUtility::toLowerCase($parameters[0]->name));
		if ($this->inputProtocol !== null && $this->inputProtocol instanceof InputProtocol) {
			return $this->inputProtocol->getInput($requestObjName);
		} else {
			throw new ProtocolException('Input protocol ' . SiteContext::$protocol . ' is not available.');
		}
	}
	
	/**
	 * @uses OutputProtocol::getOutput()
	 *
	 * @throws ProtocolException if the InputProtocol object could not be found and loaded.
	 *
	 * @return string
	 */
	public function getFormattedResponse(&$responseObj) {
		if ($this->outputProtocol !== null && $this->outputProtocol instanceof OutputProtocol) {
			return $this->outputProtocol->getOutput($responseObj);
		} else {
			throw new ProtocolException('Output protocol ' . SiteContext::$protocol . ' is not available.');
		}
	}
	
}
